package com.thl.process;


import com.thl.annotation.ServiceProcessor;
import com.thl.annotation.ServiceProcessors;
import com.thl.common.MyExcetion;
import com.thl.common.enums.BusinessScene;
import com.thl.common.enums.ErrorCodeEnum;
import com.thl.common.enums.ServiceDescription;
import com.thl.dto.ServiceContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 1900614@qq.com
 * @version 1.0
 * @date 2021/6/19 17:41
 */
@Service
public class ProcessorServiceContext implements InitializingBean, BeanPostProcessor {
    private  static  String SPLIT_KEY="#";

    private Map<String, List<Object>> processMapping=new ConcurrentHashMap<>();

    private static ProcessorServiceContext instance;
    @Override
    public void afterPropertiesSet() throws Exception {
        instance=this;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        //带了注解
        if(bean.getClass().isAnnotationPresent(ServiceProcessor.class)||bean.getClass().isAnnotationPresent(ServiceProcessors.class)){
            System.out.println("原来的"+beanName);

            regaster(bean,beanName);

        }


        return bean;
    }

    private void regaster(Object bean, String beanName) {
        //未实现抽象方法
        if(!(bean instanceof BaseProcessor)){
            throw new MyExcetion(ErrorCodeEnum.STU_SERVICE_CHECK_ERROR,"注入bean 检查 不规范");
        }
        ServiceProcessor[] annotationsByType = bean.getClass().getAnnotationsByType(ServiceProcessor.class);
        for (ServiceProcessor serviceProcessor : annotationsByType) {
            if(null==serviceProcessor){
                throw new MyExcetion(ErrorCodeEnum.STU_SERVICE_CHECK_ERROR,"注入bean  注解不能为空");
            }
            if(null==serviceProcessor.businessScene()||null==serviceProcessor.serviceDescription()){
                throw new MyExcetion(ErrorCodeEnum.STU_SERVICE_CHECK_ERROR,"注入bean  注解参数不能为空");
            }

            String key=buildServiceKey(serviceProcessor.businessScene(),serviceProcessor.serviceDescription());
            System.out.println("改后的："+key);
            List<Object> processorList=new ArrayList<>();
            if(processMapping.containsKey(key)){
                processorList=processMapping.get(key);
            }else {
                processMapping.put(key,processorList);
            }
            processorList.add(bean);

        }
    }

    private static String buildServiceKey(BusinessScene businessScene, ServiceDescription serviceDescription) {


        return businessScene.name()+SPLIT_KEY+serviceDescription.name();
    }
    public  static List<BaseProcessor> baseProcessors(ServiceContext serviceContext,ServiceDescription serviceDescription){

        if(null==instance){
            return  new ArrayList<>();
        }
        List<Object> processors = instance.processMapping.get(buildServiceKey(serviceContext.getBusinessScene(), serviceDescription));
        return (List) processors;

    }
}
